package com.zzy.banian.crm.controller;

import java.util.List;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import com.ruoyi.framework.aspectj.lang.annotation.Log;
import com.ruoyi.framework.aspectj.lang.enums.BusinessType;
import com.zzy.banian.crm.domain.Contact;
import com.zzy.banian.crm.domain.Opportunity;
import com.zzy.banian.crm.service.IOpportunityService;
import com.ruoyi.framework.web.controller.BaseController;
import com.ruoyi.framework.web.page.TableDataInfo;
import com.ruoyi.framework.web.domain.AjaxResult;
import com.ruoyi.common.utils.poi.ExcelUtil;

/**
 * 商机信息操作处理
 * 
 * @author zhangzuoyi
 * @date 2019-05-04
 */
@Controller
@RequestMapping("/crm/opportunity")
public class OpportunityController extends BaseController
{
    private String prefix = "crm/opportunity";
	
	@Autowired
	private IOpportunityService opportunityService;
	
	@RequiresPermissions("crm:opportunity:view")
	@GetMapping()
	public String opportunity()
	{
	    return prefix + "/opportunity";
	}
	
	/**
	 * 查询商机列表
	 */
	@RequiresPermissions("crm:opportunity:list")
	@PostMapping("/list")
	@ResponseBody
	public TableDataInfo list(Opportunity opportunity)
	{
		startPage();
        List<Opportunity> list = opportunityService.selectOpportunityList(opportunity);
		return getDataTable(list);
	}
	
	
	/**
	 * 导出商机列表
	 */
	@RequiresPermissions("crm:opportunity:export")
    @PostMapping("/export")
    @ResponseBody
    public AjaxResult export(Opportunity opportunity)
    {
    	List<Opportunity> list = opportunityService.selectOpportunityList(opportunity);
        ExcelUtil<Opportunity> util = new ExcelUtil<Opportunity>(Opportunity.class);
        return util.exportExcel(list, "opportunity");
    }
	
	/**
	 * 新增商机
	 */
	@GetMapping("/add")
	public String add()
	{
	    return prefix + "/add";
	}
	
	/**
	 * 新增保存商机
	 */
	@RequiresPermissions("crm:opportunity:add")
	@Log(title = "商机", businessType = BusinessType.INSERT)
	@PostMapping("/add")
	@ResponseBody
	public AjaxResult addSave(Opportunity opportunity)
	{		
		return toAjax(opportunityService.insertOpportunity(opportunity));
	}

	/**
	 * 修改商机
	 */
	@GetMapping("/edit/{id}")
	public String edit(@PathVariable("id") Integer id, ModelMap mmap)
	{
		Opportunity opportunity = opportunityService.selectOpportunityById(id);
		mmap.put("opportunity", opportunity);
	    return prefix + "/edit";
	}
	
	/**
	 * 修改保存商机
	 */
	@RequiresPermissions("crm:opportunity:edit")
	@Log(title = "商机", businessType = BusinessType.UPDATE)
	@PostMapping("/edit")
	@ResponseBody
	public AjaxResult editSave(Opportunity opportunity)
	{		
		return toAjax(opportunityService.updateOpportunity(opportunity));
	}
	
	/**
	 * 删除商机
	 */
	@RequiresPermissions("crm:opportunity:remove")
	@Log(title = "商机", businessType = BusinessType.DELETE)
	@PostMapping( "/remove")
	@ResponseBody
	public AjaxResult remove(String ids)
	{		
		return toAjax(opportunityService.deleteOpportunityByIds(ids));
	}
	@GetMapping("/findByCustomer/{customerId}")
	@ResponseBody
	public AjaxResult findByCustomer(@PathVariable("customerId") Integer customerId){
		Opportunity opportunity=new Opportunity();
		opportunity.setCustomerId(customerId);
		return AjaxResult.success(opportunityService.selectOpportunityList(opportunity));
	}
}
